package com.fcl.oschina.model;

import com.fcl.mylibrary.http.HttpClient;
import com.fcl.mylibrary.util.FLog;
import com.fcl.mylibrary.util.StringUtil;
import com.fcl.oschina.Constant;
import com.fcl.oschina.entity.BaseReturn;
import com.fcl.oschina.entity.CommentEntity;
import com.fcl.oschina.entity.ListResult;
import com.fcl.oschina.entity.TweetCommentEntity;
import com.fcl.oschina.ui.fragment.TweetCommentView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * Created by Administrator on 2017/3/22.
 */

public class TweetCommentModel {

    private String TAG = "TweetCommentModel";
    private String  mNextPage="";
    private TweetCommentView mView;

    public TweetCommentModel(TweetCommentView view) {
        mView = view;
    }

    

    public void getCommentList(int sourceId) {
        String url = String.format("%saction/apiv2/tweet_comments?sourceId=%d&pageToken=%s",
                Constant.BASE_URL, sourceId, mNextPage);
        FLog.error(TAG, "comment twteet url:"+url);
        HttpClient client = new HttpClient(url, new HttpClient.ResultListener() {
            @Override
            public void dealResult(String result) {
                Gson gson = new Gson();
                BaseReturn baseReturn = gson.fromJson(result, new TypeToken<BaseReturn<ListResult<TweetCommentEntity>>>(){}.getType());
                if (baseReturn.getCode() == 1) {
                    ListResult<TweetCommentEntity> data = (ListResult<TweetCommentEntity>) baseReturn.getResult();

                    if (data.getItems().size() == 0) {
                        mView.noMoreData();
                    } else {
                        mView.setComment(data.getItems(), !StringUtil.isEmpty(mNextPage));
                    }
                    mNextPage = data.getNextPageToken();

                }
            }
        });
        client.get();
    }

    public void setNextPage(String page) {
        mNextPage = page;
    }
}
